﻿using ETModel;
using System;

namespace Xfs
{
	public class XfsInnerMessageDispatcher : IXfsMessageDispatcher
	{
        public void Dispatch(XfsSession session, ushort opcode, object message)
        {
            this.DispatchAsync(session, opcode, message).Coroutine();
        }

        public async XfsVoid DispatchAsync(XfsSession session, ushort opcode, object message)
		{
			/// 收到actor消息,放入actor队列
			switch (message)
			{
				case IXfsActorRequest actorRequest:
					{
                        XfsEntity entity = (XfsEntity)XfsGame.EventSystem.Get(actorRequest.ActorId);

                        if (entity == null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + $"not found actor: {message}");
                            XfsActorResponse response = new XfsActorResponse
                            {
                                Error = XfsErrorCode.ERR_NotFoundActor,
                                RpcId = actorRequest.RpcId
                            };
                            session.Reply(response);
                            return;
                        }

                        XfsMailboxComponent mailBoxComponent = entity.GetComponent<XfsMailboxComponent>();
                        if (mailBoxComponent == null)
                        {
                            XfsActorResponse response = new XfsActorResponse
                            {
                                Error = XfsErrorCode.ERR_ActorNoMailBoxComponent,
                                RpcId = actorRequest.RpcId
                            };
                            session.Reply(response);
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + $"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                            return;
                        }

                        mailBoxComponent.Add(new XfsActorMessageInfo() { Session = session, Message = actorRequest });
                        break;
                    }
				case IXfsActorMessage iactorMessage:
					{
                        XfsEntity entity = (XfsEntity)XfsGame.EventSystem.Get(iactorMessage.ActorId);

                        if (entity == null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + $"not found actor: {message}");
                            return;
                        }

                        XfsMailboxComponent mailBoxComponent = entity.GetComponent<XfsMailboxComponent>();
                        if (mailBoxComponent == null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + $"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                            return;
                        }

                        mailBoxComponent.Add(new XfsActorMessageInfo() { Session = session, Message = iactorMessage });
                        break;
                    }
				   case IXfsRequest xfsRequest:  /// 分发IXfsRequest消息
                    {
                        XfsGame.XfsSence.GetComponent<XfsMessageDispatcherComponent>().Handle(session, new XfsMessageInfo() { Opcode = opcode, Message = message });

                        break;
                    }
                case IXfsMessage iMessage:  /// 分发IXfsMessage消息
                    {
                        XfsGame.XfsSence.GetComponent<XfsMessageDispatcherComponent>().Handle(session, new XfsMessageInfo() { Opcode = opcode, Message = message });

                        break;
                    }
                default:
                    {
                        /// 分发其他消息（无）

                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + " 181. 分发其他消息（无）: " + message.GetType().Name);
                        break;
                    }




            }
		}
        

    }
}
